package com.solution.admin.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductTagEntity;
import com.solution.admin.product.service.ProductTagService;
import com.solution.common.constants.PublicStatus.IS_DELETE;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品标签信息web控制层
 * 
 * @author xu_cc
 * @date 2017-10-16 16:55:10
 */
@RequestMapping("/productTag")
@Controller
public class ProductTagController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductTagController.class);

	@Autowired
	private ProductTagService productTagService;

	/**
	 * 加载商品标签下拉列表
	 * 
	 * @Title: listProductTag
	 * @Description:(描述)
	 * @param request
	 * @return List<ProductTagEntity>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月17日 下午6:26:43
	 */
	@RequestMapping("/listProductTag")
	@ResponseBody
	public List<ProductTagEntity> listProductTag(HttpServletRequest request) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("status", STATUS.ENABLED.getId());
			List<ProductTagEntity> productTagList = productTagService.listProductTag(param);
			return productTagList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 跳转商品标签信息查询页面
	 * 
	 * @param ProductTag
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-10-16 16:55:10
	 */
	@RequestMapping("/toListProductTagPage")
	public String toListProductTagPage(ProductTagEntity productTag, HttpServletRequest request, Model model, String tabIndex) {
		if (StringUtil.isEmpty(tabIndex)) {
			model.addAttribute("tabIndex", "1");
		} else {
			model.addAttribute("tabIndex", tabIndex);
		}
		return "product/productTag_list";
	}

	/**
	 * 分页查询商品标签信息列表
	 * 
	 * @param productTag
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-10-16 16:55:10
	 */
	@RequestMapping("/listProductTagPage")
	@ResponseBody
	public JSONGrid listProductTagPage(ProductTagEntity productTag, HttpServletRequest request) {
		// int pageNum =
		// Integer.parseInt(request.getParameter("page").toString());
		// int numPerPage =
		// Integer.parseInt(request.getParameter("rows").toString());
		int pageNum = 1;
		int numPerPage = 10000;
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tagName", productTag.getTagName());
		paramMap.put("status", request.getParameter("status"));
		try {
			PageBean pageBean = productTagService.listProductTagPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品标签信息失败", logger, e);
		}
		return new JSONGrid();
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return string 跳转页面路径
	 * @author xu_cc
	 * @date 2017-10-16 16:55:10
	 */
	@RequestMapping("/toAddProductTag")
	public String toAddProductTag(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productTag_form";
	}

	/**
	 * 跳转编辑页面
	 * 
	 * @return string 跳转页面路径
	 * @author xu_cc
	 * @date 2017-10-16 16:55:10
	 */
	@RequestMapping("/toUpdateProductTag")
	public String toUpdateProductTag(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductTagEntity productTag = productTagService.getProductTagById(id);
			request.setAttribute("productTag", productTag);
		} catch (Exception e) {
			handleException("跳转编辑商品标签信息页面错误", logger, e);
		}
		return "product/productTag_form";
	}

	/**
	 * 修改商品标签状态
	 * 
	 * @Title: modifyStatus
	 * @Description:(描述)
	 * @param request
	 * @param tagId
	 * @param status
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月16日 下午5:12:26
	 */
	@RequestMapping("/modifyStatus")
	@ResponseBody
	public BaseReturn modifyStatus(HttpServletRequest request, Long tagId, Integer status) {
		BaseReturn result = new BaseReturn();
		try {
			ProductTagEntity productTag = new ProductTagEntity();
			productTag.setId(tagId);
			productTag.setStatus(status);
			productTag.setModifier(getUserName());
			productTagService.updateProductTag(productTag);
			result.setCode(SysCode.SUCCESS);
			result.setMsg("商品标签状态修改成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("商品标签状态修改异常！");
			return result;
		}
	}

	/**
	 * 删除标签
	 * 
	 * @Title: doDelTag
	 * @Description:(描述)
	 * @param request
	 * @param tagId
	 * @param status
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月16日 下午5:12:26
	 */
	@RequestMapping("/doDelTag")
	@ResponseBody
	public BaseReturn doDelTag(HttpServletRequest request, Long tagId) {
		BaseReturn result = new BaseReturn();
		try {
			ProductTagEntity productTag = new ProductTagEntity();
			productTag.setId(tagId);
			productTag.setIsDelete(IS_DELETE.DELETED.getId());
			productTag.setModifier(getUserName());
			productTagService.updateProductTag(productTag);
			result.setCode(SysCode.SUCCESS);
			result.setMsg("商品标签删除成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("商品标签删除异常！");
			return result;
		}
	}

	/**
	 * 新增或保存商品标签信息
	 * 
	 * @param request
	 *            请求对象
	 * @param productTagEntity
	 *            商品标签信息实体
	 * @return
	 * @author xu_cc
	 * @date 2017-10-16 16:55:10
	 */
	@RequestMapping("/saveProductTag")
	@ResponseBody
	public BaseReturn saveProductTag(HttpServletRequest request, ProductTagEntity productTagEntity) {
		BaseReturn result = new BaseReturn();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("tagName", productTagEntity.getTagName());
			List<ProductTagEntity> list = productTagService.listProductTag(map);
			// 新增
			if (null == productTagEntity.getId() && null != list && list.size() > 0) {
				result.setCode(SysCode.FAILED);
				result.setMsg("存在相同名称的商品标签！");
				return result;
			}
			// 修改
			if (null != productTagEntity.getId() && null != list && list.size() > 0) {
				for (ProductTagEntity tag : list) {
					// 验证是否是本身
					if (!productTagEntity.getId().equals(tag.getId())) {
						result.setCode(SysCode.FAILED);
						result.setMsg("存在相同名称的商品标签！");
						return result;
					}
				}
			}
			String id = request.getParameter("id");
			if (StringUtil.isNotEmpty(id)) {
				productTagEntity.setModifier(getUserName());
				productTagService.updateProductTag(productTagEntity);
			} else {
				productTagEntity.setStatus(STATUS.ENABLED.getId());
				productTagEntity.setSortNo(1);
				productTagEntity.setCreater(getUserName());
				productTagEntity.setModifier(getUserName());
				productTagService.saveProductTag(productTagEntity);
			}
			result.setCode(SysCode.SUCCESS);
			result.setMsg("商品标签保存成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("商品标签保存异常！");
			return result;
		}
	}

}
